package ca.cbc.giscommon.entity;

import ca.cbc.giscommon.util.StringUtil;
import com.vividsolutions.jts.geom.Geometry;

import javax.persistence.*;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * Content is the table store 3rd party feed
 *
 */
@Entity
@Table(name = "content")
public class Content extends BaseEntity implements Cloneable, Serializable {

    private static final long serialVersionUID = 5901077395302964504L;
	private long id;
	private String title;
	private String link;
	private String author;
	private String description;
	private Timestamp publishDate;
//	private Geometry theGeom;
	private Source source;
	private List<Zone> zones;

	public Content() {}
	
	public Content(String title, String link, String author, String descreption,
			long publishDate, Geometry theGeom, Source source) {
		this.setTitle(StringUtil.instance.stripTags(title));
		this.setLink(StringUtil.instance.trimIfNotNull(link));
		this.setAuthor(StringUtil.instance.trimIfNotNull(author));
		this.setDescription(StringUtil.instance.stripTags(descreption));
		this.setPublishDate(new Timestamp(publishDate));
		this.setTheGeom(theGeom);
		this.setSource(source);
	}
	
	public void addZone(Zone zone) {
		if(this.getZones() == null) {
			this.zones = new ArrayList<Zone>();
		} 
		
		if(!this.getZones().contains(zone)) {
			this.zones.add(zone);
		}
	}
	
	@Override
	public Content clone() throws CloneNotSupportedException {
		return (Content) super.clone();
	}
	
	public boolean equals(Object obj) {
		if(obj.getClass().equals(Content.class) && ((Content)obj).getId() == this.getId()) {
			return true;
		}
		return false;
	}

	public String getAuthor() {
		return author;
	}

	public String getDescription() {
		return description;
	}

	@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
	public long getId() {
		return id;
	}

	public String getLink() {
		return link;
	}

	@Column(name = "pubdate")
	public Timestamp getPublishDate() {
		return publishDate;
	}

	@ManyToOne
    @JoinColumn(name = "source_id")
	public Source getSource() {
		return source;
	}

//	@Column(name = "the_geom")
//	@Type(type = "org.hibernatespatial.GeometryUserType")
//	public Geometry getTheGeom() {
//		return theGeom;
//	}

	public String getTitle() {
		return title;
	}

	@Transient
	public List<Zone> getZones() {
		return zones;
	}

	public void setAuthor(String author) {
		this.author = author;
	}
	
    public void setDescription(String description) {
		this.description = description;
	}
    
    public void setId(long id) {
		this.id = id;
	}
    
    public void setLink(String link) {
		this.link = link;
	}
    
    public void setPublishDate(Timestamp publishDate) {
		this.publishDate = publishDate;
	}

    public void setSource(Source source) {
		this.source = source;
	}

//	public void setTheGeom(Geometry theGeom) {
//		this.theGeom = theGeom;
//	}

	public void setTitle(String title) {
		this.title = title;
	}
	
	public void setZones(List<Zone> zones) {
		this.zones = zones;
	}

	public String toString() {
		StringBuilder zonesSB = null;
		if(zones != null) {
			zonesSB = new StringBuilder("{");
			for(Zone zone : zones) {
				zonesSB.append(zone.toString()).append(", ");
			}
			zonesSB.append("}");
		}
		
    	StringBuilder sf = new StringBuilder();
    	sf.append("Content {id: ").append(id)
    	  .append(", title:").append(title)
    	  .append(", description:").append(description)
    	  .append(", link:").append(link)
    	  .append(", publishDate:").append(publishDate)
    	  .append(", source:").append(source)
    	  .append(", zones:").append(zonesSB == null ? null : zonesSB.toString())
    	  .append(", theGome:").append(theGeom)
    	  .append("}");
    	
    	return sf.toString();
    }
}
